/**
 * 作业回顾：把静态资源(css img  js 音频)通过url展示出来
 * 
 * 首先要明确url(也称为统一资源定位符)
 * 
 * url 比方 http://www.baidu.com/imgs/2022-3-8/demo.png?page=1&show=true
 * 
 * www.baidu.com host 域名(指定ip)可获取使用这个ip的电脑设备
 * 
 * url :  http://www.baidu.com/imgs/2022-3-8/demo.png?page=1&show=true
 * 
 * uri : imgs/2022-3-8/demo.png?page=1&show=true
 * 
 * path: imgs/2022-3-8/demo.png 可通过path指定计算机的资源
 * 
 * 
 * 
 */

let http = require("http");

let fs = require("fs");
const { url } = require("inspector");

let server = http.createServer();

server.on('request',function(req,res){

    //url http://localhost:8080/imgs/demo.png?page=1

    console.log(req.url);

    if(req.url.lastIndexOf(".")>-1){
        let path = req.url.split("?")[0];//获取到path路径 /imgs/demo.png

        path='.'+path;//  ./imgs/demo.png
        if(fs.existsSync(path)){
            fs.readFile(path,function(err,data){
                console.log(data);
                res.write(data);
                res.end();
        
            })
        }
       
    }

   

    
});

server.listen(8080);


